<html>
<head>
<title>this is javascript demo</title>
<script type="text/javascript">
	var Quo=function(string) {
		this.status=string
	};

	Quo.prototype.get_status=function() {
		return this.status;
	};

	var myQuo=new Quo("confused");

	document.writeln(myQuo.get_status());

	var add = function(a,b) {
		if (typeof a !== 'number' || typeof b !== 'number') {
			throw {
				name: 'TypeError',
				message: 'A number args needed'
			};
		}
		return a+b;
	};

	var array = [3,4];

	var sum = add.apply(null,array);
	document.writeln(sum);

	var statusObject = {
		status:'A-OK'
	};

	var status = Quo.prototype.get_status.apply(statusObject);

	document.writeln(status);

	var sum = function() {
		var i,sum=0;
		for (var i = 0; i < arguments.length; i++) {
			sum+=arguments[i]
		}
		return sum;
	};
	document.writeln(sum(1,2,3,4,5));

	var try_it = function() {
		try {
			add(2,'jj');
		}catch(e){
			document.writeln(e.name+":"+e.message);
		}
	};

	try_it();

	Function.prototype.method=function(name,func) {
		this.prototype[name]=func;
	};


	Number.method('integer',function() {
		return (this>0)?Math.floor(this):Math.ceil(this);
	});

	document.writeln((-10/3).integer());

	String.method('trim',function() {
		return this.replace(/^\s+|\s+$/g,'');
	});

	document.writeln('"'+"     chen        ".trim()+'"');

	var myObject = function() {
		var value=0;
		return {
			increment: function(inc) {
				value += typeof inc === 'number' ? inc:1;
			},
			getValue: function() {
				return value;
			}
		}
	}();

	myObject.increment();
	myObject.increment();

	document.writeln(myObject.getValue());

	var quo = function(status) {
		return {
			get_status: function() {
				return status;
			}
		};
	};

	var myquo = quo("amazed");
	document.writeln(myquo.get_status());

	var fade = function(node) {
		var level = 1;
		var step = function() {
			var hex = level.toString(16);
			node.style.backgroundColor = '#FFFF'+hex+hex;
			if (level<15) {
				level += 1;
				setTimeout(step, 100);
			}
		};
		setTimeout(step,100);
	};

	fade(document.body);

</script>
</head>


<body>
	<h1>这是一个javascript学习练习</h1>
</body>
</html>